package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    NursingPlanMapper nursingPlanMapper;
    @Autowired
    NursingProjectPlanService nursingProjectPlanService;

    @Override
    public List<NursingPlanVo> query() {
        List<NursingPlanVo> nursingPlans=nursingPlanMapper.query();
        return nursingPlans;

    }

    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan bean = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        List<ProjectPlan> projectPlans = bean.getProjectPlans();
        nursingPlanMapper.add(bean);
        projectPlans.forEach(projectPlan -> projectPlan.setPlanId(bean.getId()));
        projectPlans.forEach(projectPlan -> nursingProjectPlanService.add(projectPlan));


    }

    @Override
    public PageResponse<NursingPlanVo> search(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> nursingPlans =nursingPlanMapper.search(name, pageNum, pageSize, status);
        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = PageResponse.of(nursingPlans, NursingPlanVo.class);
        return nursingPlanVoPageResponse;
    }

    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlanVo nursingPlan=nursingPlanMapper.getById(id);
        return nursingPlan;
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.update(nursingPlan);
    }

    @Override
    public void delete(Long id) {
        nursingPlanMapper.delete(id);
        nursingProjectPlanService.deleteByNursingPlanId(id);
    }

    @Override
    public void statusAlter(Long id, Integer status) {
        nursingPlanMapper.statusAlter(id,status);
    }
}
